import java.util.*;

public class TreeNode {
    int val = 0;
    TreeNode left = null;
    TreeNode right = null;
    public TreeNode(int val) {
        this.val = val;
    }
}

public class Solution
{
    public boolean IsBalanced_Solution (TreeNode pRoot)
    {
        return dfs(pRoot) != -1;
    }
    public int dfs(TreeNode root)  {// 当返回值不是 -1 的时候，返回的是树的⾼度
        if(root == null)
            return 0;
        int left = dfs(root.left);
        if(left == -1)
            return -1; // 剪枝
        int right = dfs(root.right);
        if(right == -1)
            return -1; // 剪枝
        return Math.abs(left - right) <= 1 ? Math.max(left, right) + 1 : -1;
    }
}
